import { defineStore } from "pinia"
import { selectToken, selectUserInfo, selectRouterList, deleteToken } from "@/api/login"
import cache from "@/utils/cache"
import router, { routes } from "@/router"
import { mapMenuToRoutes } from "@/utils/router"

export const useLoginStore = defineStore('loginStore', {
    state: () => {
        return {
            // token
            token: cache.getCache("token"),
            // 用户信息
            userInfo: {},
            // 用户角色
            roles: [],
            // 权限菜单 专题菜单
            menuList: [
                {
                    id: "1",
                    title: "全域客流",
                    icon: "flow",
                    path: "/flow",
                    component: "Layout",
                    children: [
                        {
                            id: "2",
                            title: "客流分析",
                            path: "analysis",
                            component: "flow/analysis/analysis",
                            meta: {
                                isShowMap: true
                            }
                        },
                        {
                            id: "4",
                            title: "游客画像",
                            path: "portrait",
                            component: "flow/portrait/portrait",
                            meta: {
                                isShowMap: false
                            }
                        },
                        {
                            id: "3",
                            title: "客流预警",
                            path: "warn",
                            component: "flow/warn/warn",
                            meta: {
                                isShowMap: true
                            }
                        }
                    ]
                },
                {
                    id: "5",
                    title: "旅游交通",
                    icon: "travel",
                    path: "/travel",
                    component: "Layout",
                    children: [
                        {
                            id: "77",
                            title: "旅游交通",
                            path: "travel",
                            component: "travel/travel",
                            meta: {
                                isShowMap: true
                            }
                        }
                    ],
                    meta: {
                        isShowMap: true
                    }
                },
                {
                    id: "6",
                    title: "应急指挥",
                    icon: "control",
                    path: "/control",
                    component: "Layout",
                    children: [
                        {
                            id: "7",
                            title: "指挥管理",
                            path: "command",
                            component: "control/command/command",
                            meta: {
                                isShowMap: true
                            }
                        },
                        {
                            id: "8",
                            title: "预警管理",
                            path: "plan",
                            component: "control/plan/plan",
                            meta: {
                                isShowMap: false
                            }
                        }
                    ]
                },
                {
                    id: "9",
                    title: "舆情分析",
                    icon: "situation",
                    path: "/situation",
                    component: "Layout",
                    children: [
                        {
                            id: "99",
                            title: "舆情分析",
                            path: "situation",
                            component: "situation/situation"
                        }
                    ],
                    meta: {
                        isShowMap: true
                    }
                },
                {
                    id: "10",
                    title: "投诉监督",
                    icon: "complaint",
                    path: "/complaint",
                    component: "Layout",
                    children: [
                        {
                            id: "11",
                            title: "投诉分析",
                            path: "complain",
                            component: "complaint/complain/complain",
                            meta: {
                                isShowMap: true
                            }
                        },
                        {
                            id: "12",
                            title: "客服分析",
                            path: "customer",
                            component: "complaint/customer/customer",
                            meta: {
                                isShowMap: true
                            }
                        }
                    ]
                },
                {
                    id: "13",
                    title: "渠道运营",
                    icon: "channel",
                    path: "/channel",
                    component: "Layout",
                    children: [],
                    meta: {
                        isShowMap: true
                    }
                },
                {
                    id: "14",
                    title: "市场监测",
                    icon: "market",
                    path: "/market",
                    component: "Layout",
                    children: [
                        {
                            id: "15",
                            title: "旅游资源监测",
                            path: "resource",
                            component: "market/resource/resource",
                            meta: {
                                isShowMap: true
                            }
                        },
                        {
                            id: "16",
                            title: "景区监测",
                            path: "scenic",
                            component: "market/scenic/scenic",
                            meta: {
                                isShowMap: true
                            }
                        },
                        {
                            id: "17",
                            title: "酒店监测",
                            path: "hotel",
                            component: "market/hotel/hotel",
                            meta: {
                                isShowMap: true
                            }
                        },
                        {
                            id: "217",
                            title: "旅行社",
                            path: "agency",
                            component: "market/agency/agency",
                            meta: {
                                isShowMap: true
                            }
                        },
                        {
                            id: "18",
                            title: "文化市场监测",
                            path: "culture",
                            component: "market/culture/culture",
                            meta: {
                                isShowMap: true
                            }
                        }
                    ]
                },
                {
                    id: "19",
                    title: "产业消费",
                    icon: "industry",
                    path: "/industry",
                    component: "Layout",
                    children: [
                        {
                            id: "20",
                            title: "总体消费",
                            path: "overall",
                            component: "industry/overall/overall",
                            meta: {
                                isShowMap: true
                            }
                        },
                        {
                            id: "21",
                            title: "消费画像",
                            path: "consumption",
                            component: "industry/consumption/consumption",
                            meta: {
                                isShowMap: true
                            }
                        },
                        {
                            id: "22",
                            title: "景区收入分析",
                            path: "income",
                            component: "industry/income/income",
                            meta: {
                                isShowMap: true
                            }
                        }
                    ]
                },
                {
                    id: "23",
                    title: "智慧服务",
                    icon: "wisdom",
                    path: "/wisdom",
                    component: "Layout",
                    children: [
                        {
                            id: "24",
                            title: "景区服务",
                            path: "service",
                            component: "wisdom/service/service",
                            meta: {
                                isShowMap: true
                            }
                        },
                        {
                            id: "25",
                            title: "甄选特产",
                            path: "product",
                            component: "wisdom/product/product",
                            meta: {
                                isShowMap: true
                            }
                        },
                        {
                            id: "26",
                            title: "展馆服务",
                            path: "show",
                            component: "wisdom/show/show",
                            meta: {
                                isShowMap: true
                            }
                        }
                    ]
                },
                {
                    id: "27",
                    title: "信用企业",
                    icon: "credit",
                    path: "/credit",
                    component: "Layout",
                    children: [
                        {
                            id: "277",
                            title: "信用企业",
                            path: "credit",
                            component: "credit/credit",
                            meta: {
                                isShowMap: false
                            }
                        }
                    ],
                    meta: {
                        isShowMap: true
                    }
                },
                {
                    id: "28",
                    title: "一机游分析",
                    icon: "machine",
                    path: "/machine",
                    component: "Layout",
                    children: [
                        {
                            id: "288",
                            title: "一机游分析",
                            path: "machine",
                            component: "machine/machine"
                        }
                    ],
                    meta: {
                        isShowMap: true
                    }
                }
            ],
        }
    },
    actions: {
        // 获取token
        getToken(form) {
            return new Promise((resolve, reject) => {
                selectToken(form).then(res => {
                    this.token = res.data.access_token
                    cache.setCache("token", res.data.access_token)
                    resolve(res);
                }).catch(err => {
                    reject(err);
                })
            })
        },
        // 获取用户信息
        getUserInfo() {
            return new Promise((resolve, reject) => {
                selectUserInfo().then((res) => {
                    this.roles = res.roles
                    this.userInfo = res.user
                    resolve(res)
                }).catch(err => {
                    reject(err);
                })
            })
        },
        // 获取专题菜单
        getMenusList() {
            return new Promise((resolve, reject) => {
                selectRouterList().then((res) => {
                    resolve(res)
                }).catch(err => {
                    reject(err);
                })
            })
        },
        // 注册动态路由
        registerDynamicRoutes() {
            const dynamicRoutes = mapMenuToRoutes(this.menuList)
            // 防止/找不到子路由
            routes.forEach(item => {
                if (item.path == "/") {
                    item.redirect = this.getDefaultPath()
                }
            })
            // 注册路由
            dynamicRoutes.forEach(route => {
                router.addRoute(route)
            })
        },
        // 退出登录
        logout() {
            return new Promise((resolve, reject) => {
                deleteToken().then((res) => {
                    this.reset()
                    resolve(res)
                }).catch(err => {
                    reject(err);
                })
            })
        },
        // 获取登录成功默认跳转的路由地址
        getDefaultPath() {
            const firstMenu = this.menuList[0]

            if (firstMenu.children && firstMenu.children.length > 0) {
                return `${firstMenu.path}/${firstMenu.children[0].path}`
            } else {
                return firstMenu.path
            }
        },
        // 重置
        reset() {
            cache.clearCache()
            this.token = ""
            this.userInfo = {}
            this.roles = []
        }
    }
})